package A1_4_Shopsystem;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 订单类
public class Order {
    private Date createDate;
    private OrderStatus status;
    private Customer customer;           // 关联关系：订单属于一个客户
    private List<OrderDetail> orderDetails; // 组合关系：订单包含多个订单详情
    private Payment payment;             // 关联关系：订单对应一个支付

    public Order(Customer customer) {
        this.createDate = new Date();
        this.status = OrderStatus.CREATE;
        this.customer = customer;
        this.orderDetails = new ArrayList<>();
        customer.addOrder(this); // 建立双向关联
    }

    // 添加订单详情
    public void addOrderDetail(Product product, int quantity) {
        OrderDetail orderDetail = new OrderDetail(quantity, product, this);
        this.orderDetails.add(orderDetail);
    }

    // 计算订单总金额
    public double calculateTotalAmount() {
        return orderDetails.stream()
                .mapToDouble(OrderDetail::calculateSubTotal)
                .sum();
    }

    // 计算订单总重量
    public double calculateTotalWeight() {
        return orderDetails.stream()
                .mapToDouble(OrderDetail::calculateWeight)
                .sum();
    }

    // 设置支付
    public void setPayment(Payment payment) {
        this.payment = payment;
        this.status = OrderStatus.PAID; // 支付后更新状态
    }

    // Getter 和 Setter 方法
    public Date getCreateDate() {
        return createDate;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public Customer getCustomer() {
        return customer;
    }

    public List<OrderDetail> getOrderDetails() {
        return orderDetails;
    }

    public Payment getPayment() {
        return payment;
    }
}
